﻿using DocumentFormat.OpenXml.Office2010.Excel;
using DocumentFormat.OpenXml.Wordprocessing;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Excel;
using NP.BPMReportPlatform.BPMPlatform;
using NP.BPMReportPlatform.DBHelper;
using NP.BPMReportPlatform.Entities.BPM_Business;
using NP.BPMReportPlatform.Entities.BPM_Business.SWSendMail;
using NP.BPMReportPlatform.Entities.BPM_Platform;
using NP.BPMReportPlatform.FrameOrder.Dto;
using NP.BPMReportPlatform.HandInvoice.Dto;
using NP.BPMReportPlatform.HR.Dto;
using NP.BPMReportPlatform.RecurringJobs;
using NP.BPMReportPlatform.SWSendMail.Dto;
using NP.BPMReportPlatform.TaxManage.Dto;
using NP.BPMReportPlatform.Utils;
using Org.BouncyCastle.Crypto.Tls;
using SqlSugar;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.ObjectMapping;

namespace NP.BPMReportPlatform.SWSendMail
{

    public class SWSendMailService : BPMReportPlatformAppService, ISWSendMailService
    {
        private readonly SqlSugarClient _dbBusiness;
        private readonly IBPMPlatformAppService _iBPMPlatformApp;
        private readonly IBPMUtilsAppService _bPMUtilsApp;
        public SWSendMailService(ISqlSugarAppService sqlSugarAppService
            , IBPMPlatformAppService iBPMPlatformApp
            , IBPMUtilsAppService bPMUtilsApp)
        {
            _dbBusiness = sqlSugarAppService.GetBusinessInstance();
            _iBPMPlatformApp = iBPMPlatformApp;
            _bPMUtilsApp = bPMUtilsApp;
        }

        public async Task<PagedResultDto<SearchOutPutDto>> GetAllAsync(SearchInputDto input)
        {
            RefAsync<int> totalCount = 0;
            var result = new PagedResultDto<SearchOutPutDto>();
            try
            {
                var getQuery = _getAll(input);
                var mapperList = await getQuery.ToPageListAsync(input.PageIndex, input.PageSize, totalCount);
                await AssignList(mapperList);
                result.TotalCount = totalCount;
                result.Items = mapperList;
            }
            catch (Exception EX)
            {

                throw;
            }

            return result;
        }

        public async Task<List<SearchOutPutDto>> ExportAsync(SearchInputDto input)
        {
            var listQueryable = _getAll(input);
            var listViewDto = await listQueryable.ToListAsync();
            return listViewDto;
        }

        private ISugarQueryable<SearchOutPutDto> _getAll(SearchInputDto input)
        {

            var result = _dbBusiness.Queryable<SWSendMailSearchData, SWSendMailSelected>((a, b) => new JoinQueryInfos(JoinType.Inner, a.InstanceNumber == b.InstanceNumber))
                .WhereIF(!string.IsNullOrEmpty(input.InstanceNumber), (a, b) => a.InstanceNumber.Contains(input.InstanceNumber))
                .WhereIF(!string.IsNullOrEmpty(input.SeachUser), (a, b) => a.AddUserName.Contains(input.SeachUser)
                || a.workNumber.Contains(input.SeachUser)
                || a.loginName.Contains(input.SeachUser))
                .WhereIF(!string.IsNullOrEmpty(input.SearchelectricInvoiceCode), (a, b) => a.electricInvoiceCode.Contains(input.SearchelectricInvoiceCode)
                || b.electricInvoiceCode.Contains(input.SearchelectricInvoiceCode))
                .WhereIF(input.BeginTime.HasValue && input.EndTime.HasValue, (a, b) => a.AddTime >= input.BeginTime && a.AddTime <= input.EndTime)
                    .Select((a, b) => new SearchOutPutDto()
                    {
                        InstanceNumber = a.InstanceNumber,
                        UserName = a.AddUserName,
                        bossName = a.bossName,
                        goumaiName = a.goumaiName,
                        xiaofangCompanyCode = a.xiaofangCompanyCode,
                        goumaiCompanyCode = a.goumaiCompanyCode,
                        xiaofangName = a.xiaofangName,
                        AddTime = a.AddTime.ToString("yyyy-MM-dd"),
                        SearchelectricInvoiceCode = a.electricInvoiceCode,
                        //invoiceDate = SqlFunc.GetDate().ToShortDateString(),
                        endDate = a.endDate,
                        beginDate = a.beginDate,
                        sellerCompanyCode = b.sellerCompanyCode,
                        sellerName = b.sellerName,
                        buyerName = b.buyerName,
                        buyerCompanyCode = b.buyerCompanyCode,
                        SendelectricInvoiceCode = b.electricInvoiceCode,

                    }).OrderBy(a => a.AddTime, OrderByType.Desc);
            return result;
        }

        private async Task<List<SearchOutPutDto>> AssignList(List<SearchOutPutDto> listViewDto)
        {

            var userId = await _iBPMPlatformApp.GetUserAsync(CurrentUser.UserName);

            listViewDto.ForEach(x =>
            {
                x.Url = _bPMUtilsApp.GetProcessUrl(x.InstanceNumber, userId);
            }
            );
            return listViewDto;
        }
    }
}
